from django.db import models
from django.utils import timezone
from user.models import User, Role


class Language(models.Model):

    name = models.CharField(max_length=30, blank=True, null=True, db_comment="语言名称")

    description = models.TextField(blank=True, null=True, db_comment="语言描述")

    create_time = models.DateTimeField(db_comment="添加时间", blank=True, null=True, default=timezone.now)

    update_time = models.DateTimeField(db_comment="修改时间", blank=True, null=True)

    delete_time = models.DateTimeField(db_comment="删除时间", blank=True, null=True)

    is_delete = models.BooleanField(db_comment="是否删除", blank=True, null=True, default=False)

    is_active = models.BooleanField(default=True, blank=True, db_comment="是否启用")

    user = models.ForeignKey(to=User, on_delete=models.CASCADE, null=True, blank=True, related_name="languages")

    class Meta:
        db_table = 'tb_language'


class Module(models.Model):

    name = models.CharField(max_length=30, blank=True, null=True, db_comment="模块名称")

    description = models.TextField(blank=True, null=True, db_comment="模块描述")

    create_time = models.DateTimeField(db_comment="添加时间", blank=True, null=True, default=timezone.now)

    update_time = models.DateTimeField(db_comment="修改时间", blank=True, null=True)

    delete_time = models.DateTimeField(db_comment="删除时间", blank=True, null=True)

    is_delete = models.BooleanField(db_comment="是否删除", blank=True, null=True, default=False)

    is_active = models.BooleanField(default=True, blank=True, db_comment="是否启用")

    language = models.ForeignKey(to=Language, on_delete=models.DO_NOTHING, blank=True, null=True, related_name='modules')

    user = models.ForeignKey(to=User, on_delete=models.CASCADE, null=True, blank=True)

    class Meta:
        db_table = 'tb_module'


class Task(models.Model):

    # 任务名称
    title = models.TextField(blank=True, db_comment="任务名")

    # 难易程度
    completed = models.CharField(
        max_length=20,
        choices=[
        ('简单题', '简单题'),
        ('中等题', '中等题'),
        ('难题', '难题'),
        ('选做题', '选做题'),
        ],
        db_comment="难易程度",
        blank=True,
    )

    # 任务时长
    task_time = models.CharField(max_length=30, blank=True, null=True, db_comment="任务时长，时间+单位")

    # 任务状态
    status = models.IntegerField(
        choices=[
            (0, '未开始'),
            (1, '进行中'),
            (2, '需讲解'),
            (3, '已提交'),
            (4, '已检查'),
            (-1, '异常题'),
            (5, '已完成'),
        ],
        db_comment="任务状态",
        blank=True, null=True, default=0
    )

    create_time = models.DateTimeField(db_comment="添加时间", blank = True, null = True, default = timezone.now)

    delete_time = models.DateTimeField(db_comment="删除时间", blank = True, null = True)

    is_delete = models.BooleanField(db_comment="是否删除", blank = True, null = True, default = False)

    update_time = models.DateTimeField(db_comment="修改时间", blank=True, null=True)

    language = models.ForeignKey(to=Language, on_delete=models.DO_NOTHING, blank=True, null=True, related_name='tasks')

    module = models.ForeignKey(to=Module, on_delete=models.DO_NOTHING, blank=True, null=True, related_name='tasks')

    # 多对多关联关系的维护
    users = models.ManyToManyField(to=User, through='TaskUser', related_name="tasks")

    class Meta:
        db_table = 'tb_task'

# 学生任务表
class TaskUser(models.Model):

    task = models.ForeignKey(to=Task, on_delete=models.CASCADE, null=True, blank=True)

    user = models.ForeignKey(to=User, on_delete=models.CASCADE, null=True, blank=True)

    answer = models.TextField(null=True, blank=True, db_comment="答案")

    # 是否会做
    is_solve = models.BooleanField(default=True, blank=True, db_comment="是否会做")

    # 是否检查
    is_check = models.BooleanField(default=False, blank=True, db_comment="是否检查")


    class Meta:
        db_table = "tb_task_user"
        unique_together = ("task", "user")




# 人员组表
class Group(models.Model):

    name = models.CharField(max_length=50, db_comment="组名", blank=True, null=True)

    create_time = models.DateTimeField(db_comment="添加时间", blank = True, null = True, default = timezone.now)

    # 多对多关联关系的维护
    users = models.ManyToManyField(to=User, through='GroupUser', related_name="group")

    class Meta:
        db_table = 'tb_group'

# 学生分组表
class GroupUser(models.Model):

    group = models.ForeignKey(to=Group, on_delete=models.CASCADE, null=True, blank=True)

    user = models.ForeignKey(to=User, on_delete=models.CASCADE, null=True, blank=True)

    class Meta:
        db_table = "tb_group_user"
        unique_together = ("group", "user")




class Menu(models.Model):

    menu_name = models.CharField(max_length=20, db_comment='菜单名称', null=True, blank=True)

    icon = models.CharField(max_length=20, db_comment='菜单图标', null=True, blank=True)

    description = models.CharField(max_length=50, db_comment='菜单描述', null=True, blank=True)

    sort = models.IntegerField(db_comment='排序', null=True, blank=True)

    create_time = models.DateTimeField(db_comment='创建时间', null=True, blank=True, default=timezone.now)

    state = models.BooleanField(db_comment="状态", null=True, blank=True)

    update_time = models.DateTimeField(db_comment='修改时间', null=True, blank=True)

    delete_time = models.DateTimeField(db_comment='删除时间', null=True, blank=True)

    is_delete = models.BooleanField(db_comment='是否删除', null=True, blank=True, default=False)

    class Meta:
        db_table = 'tb_menu'


class Func(models.Model):

    func_name = models.CharField(max_length=20, db_comment='功能名称', null=True, blank=True)

    func_code = models.CharField(max_length=50, db_comment='功能编码', null=True, blank=True)

    func_url = models.CharField(max_length=50, db_comment='功能地址', null=True, blank=True)

    description = models.CharField(max_length=20, db_comment='功能描述', null=True, blank=True)

    create_time = models.DateTimeField(db_comment='创建时间', null=True, blank=True, default=timezone.now)

    state = models.BooleanField(db_comment="状态", null=True, blank=True)

    update_time = models.DateTimeField(db_comment='修改时间', null=True, blank=True)

    delete_time = models.DateTimeField(db_comment='删除时间', null=True, blank=True)

    is_delete = models.BooleanField(db_comment='是否删除', null=True, blank=True, default=False)

    menu_id = models.ForeignKey(to=Menu, on_delete=models.DO_NOTHING, db_comment="所属菜单", blank=True, null=True, related_name="funcs")

    class Meta:
        db_table = 'tb_func'


class Permission(models.Model):
    role_id = models.ForeignKey(to=Role, on_delete=models.CASCADE, db_comment="角色", related_name="permissions")
    menu_id = models.ForeignKey(to=Menu, on_delete=models.CASCADE, db_comment="菜单", related_name="permissions")
    func_id = models.ForeignKey(to=Func, on_delete=models.CASCADE, db_comment="功能", related_name="permissions")

    create_time = models.DateTimeField(default=timezone.now, db_comment="创建时间")

    class Meta:
        db_table = 'tb_permission'
        unique_together = ('role_id', 'menu_id', 'func_id')



